package baseGUI;

/*This program is just a base sample code for cardLayout which will
 * be implemented in the program. The 'public class [filename] extends
 * JFrame is a must. Each GUI which is copied from netBeans has the 
 * starting 'public class .....'. Paste the code outside of the class 
 * file and delete the 'public'. Each constructor of the new panel(GUI)
 * must have a parameter 'JPanel [panel_name]'.
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BaseGUI extends JFrame {
    private JPanel panel;
    
    public BaseGUI() {
    	initComponents();
    }
    
    //Function to initialise the frame for the program
    private void initComponents () {
        setTitle("Card Layout Example");
        setSize(1000, 700);
        
        //Any extra GUI to be linked must be listed here.
        //The panels have listed 3 GUIs.
        //The format is [JPanel name].add(new [GUI class
        //   constructor([JPanel name]), "[any name for the GUI]");
        panel = new JPanel(new CardLayout());
        panel.add(new MainMenu(panel), "mainMenu");
        panel.add(new firstGUI(panel), "firstGUI");
        panel.add(new secondGUI(panel), "secondGUI");
        this.getContentPane().add(panel);
    }
    
    public static void main(String[] args) {
    	EventQueue.invokeLater(new Runnable() {
    		public void run() {
    			new BaseGUI().setVisible(true);
    		}
    	});
    }
}

//The very first GUI (This GUI will appear when the program executes
class MainMenu extends JPanel {	
	private JPanel mainPanel;
	
	public MainMenu(JPanel panel) {
		mainPanel = panel;
        
		JButton button1 = new JButton("First GUI");
		button1.addActionListener(new PanelSwitchTofirstGUI());
		this.add(button1);
		
		JButton button2 = new JButton("Second GUI");
		button2.addActionListener(new PanelSwitchTosecondGUI());
		this.add(button2);
	}
	
	//class to switch back to the Main Menu when the button is clicked
	class PanelSwitchTofirstGUI implements ActionListener {
		public void actionPerformed(ActionEvent ae) {
			CardLayout c = (CardLayout)(mainPanel.getLayout());
			c.show(mainPanel, "firstGUI");
		}
	}
	
	//class to switch to the second GUI when the button is clicked
	class PanelSwitchTosecondGUI implements ActionListener {
		public void actionPerformed(ActionEvent ae) {
			CardLayout c = (CardLayout)(mainPanel.getLayout());
			c.show(mainPanel, "secondGUI");
		}
	}
}


//-------------------------------------------------------------------//
//The GUI called 'firstGUI'
class firstGUI extends JPanel {
    private JPanel mainPanel;

    public firstGUI(JPanel panel) {
        mainPanel = panel;
        initComponents();
    }

    private void initComponents() {

        jLabel1 = new JLabel();
        returning = new JButton();
        jButton2 = new JButton();

        jLabel1.setText("First GUI");
        
        //A button that will display the Main Menu when is clicked.
        //The ActionListener is put here.
        //Don't use the netBeans add events for the buttons that will
        //  display another panel.
        returning.setText("Main Menu");
        returning.addActionListener(new PanelSwitchToMenu());
        
        //A button that will display the GUI called 'second GUI'.
        jButton2.setText("Second GUI");
        jButton2.addActionListener(new PanelSwitchTosecondGUI());
        
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(160, 160, 160)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(143, 143, 143)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2)
                            .addComponent(returning))))
                .addContainerGap(184, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1)
                .addGap(44, 44, 44)
                .addComponent(returning)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addContainerGap(162, Short.MAX_VALUE))
        );
    }
    
    //The following 2 classes is a must to be included in each class.
    //They will display the panel(GUI) to be displayed as intended.
    class PanelSwitchToMenu implements ActionListener {
    	public void actionPerformed(ActionEvent ae) {
    		CardLayout c = (CardLayout)(mainPanel.getLayout());
    		c.show(mainPanel, "mainMenu");
		}
    }
    
    class PanelSwitchTosecondGUI implements ActionListener {
    	public void actionPerformed(ActionEvent ae) {
    		CardLayout c = (CardLayout)(mainPanel.getLayout());
    		c.show(mainPanel, "secondGUI");
		}
    }
    
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton returning;
}


//-------------------------------------------------------------------//
//The GUI called 'second GUI'
class secondGUI extends JPanel {
    private JPanel mainPanel;

    public secondGUI(JPanel panel) {
        mainPanel = panel;
        initComponents();
    }

    private void initComponents() {

        jLabel1 = new JLabel();
        returning = new JButton();
        jButton2 = new JButton();

        jLabel1.setText("WELCOME TO THE SECOND GUI");

        returning.setText("Main Menu");
        returning.addActionListener(new PanelSwitchToMenu());
        
        jButton2.setText("First GUI");
        jButton2.addActionListener(new PanelSwitchTofirstGUI());
        
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(160, 160, 160)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(143, 143, 143)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2)
                            .addComponent(returning))))
                .addContainerGap(184, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1)
                .addGap(44, 44, 44)
                .addComponent(returning)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addContainerGap(162, Short.MAX_VALUE))
        );
    }
    
    
    class PanelSwitchToMenu implements ActionListener {
    	public void actionPerformed(ActionEvent ae) {
    		CardLayout c = (CardLayout)(mainPanel.getLayout());
    		c.show(mainPanel, "mainMenu");
		}
    }
    
    class PanelSwitchTofirstGUI implements ActionListener {
    	public void actionPerformed(ActionEvent ae) {
    		CardLayout c = (CardLayout)(mainPanel.getLayout());
    		c.show(mainPanel, "firstGUI");
		}
    }
    
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton returning;
}